package com.itcg.springboot01change.service.impl;

import com.itcg.springboot01change.mapper.ProductMapper;
import com.itcg.springboot01change.pojo.Page;
import com.itcg.springboot01change.pojo.Product;
import com.itcg.springboot01change.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product getById(Integer id) {
        return productMapper.getById(id);
    }

    @Override
    public List<Product> getByCondition(Map<String, Object> params) {
        return productMapper.getByCondition(params);
    }

    @Override
    public List<Product> getAll() {
        return productMapper.getAll();
    }

    @Override
    public int insert(Product product) {
        return productMapper.insert(product);
    }

    @Override
    public int batchInsert(List<Product> productList) {
        return productMapper.batchInsert(productList);
    }

    @Override
    public int update(Product product) {
        return productMapper.update(product);
    }

    @Override
    public int dynamicUpdate(Map<String, Object> params) {
        return productMapper.dynamicUpdate(params);
    }

    @Override
    public int delete(Integer id) {
        return productMapper.delete(id);
    }

    @Override
    public int batchDelete(List<Integer> ids) {
        return productMapper.batchDelete(ids);
    }

    @Override
    public int deleteByCondition(Map<String, Object> params) {
        return productMapper.deleteByCondition(params);
    }

    @Override
    public List<Product> getByPage(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);
        if (pageNum <= 0) pageNum = 1;
        if (pageSize <= 0) pageSize = 10;
        int offset = (pageNum - 1) * pageSize;
        params.put("offset", offset);
        params.put("pageSize", pageSize);
        return productMapper.getByPage(params);
    }

    @Override
    public int count(Map<String, Object> params) {
        return productMapper.count(params);
    }
}
